package com.study.happystudy.service.impl;

import com.study.happystudy.mapper.PsyTestRecordMapper;
import com.study.happystudy.po.PsyTestRecord;
import com.study.happystudy.service.PsyTestRecordService;
import com.study.happystudy.tool.ColorChange;
import com.study.happystudy.tool.PsyTestHelp;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PsyTestRecordServiceImpl implements PsyTestRecordService {
    @Resource
    PsyTestRecordMapper psyTestRecordMapper;

    @Override
    public PsyTestRecord insert(int userId, int correct) {
        int score = (int) ((correct * 1.0 / 20) * 100 + 0.5);
        ColorChange.Blue(score + "\n");

        String result;
        if (score >= 80) {
            result = "健康";
        } else {
            int rdm = (int) (Math.random() * 1000);
            String symptom = rdm % 4 != 0 ? "焦虑" : "抑郁";
            if (score >= 60) {
                result = "轻度" + symptom;
            } else if (score >= 30) {
                result = "中度" + symptom;
            } else {
                result = "重度" + symptom;
            }
        }

        // 鸡汤
        String ckp;
        final String dft = "不要为别人的成功而眼红，因为别人的成功并不代表你也能成功。";
        try {
            ckp = PsyTestHelp.getChickenSoup();
            if (ckp == null) ckp = dft;
        } catch (Exception e) {
            ColorChange.Red(e.getMessage() + "\n");
            ckp = dft;
        }

        String advice = PsyTestHelp.getAdvice(result);
        String analysis = PsyTestHelp.getAnalysis(result);

        psyTestRecordMapper.insert(userId, score, result, analysis, advice, ckp);
        return new PsyTestRecord(userId, score, result, analysis, advice, ckp);
    }


    @Override
    public List<PsyTestRecord> selectByUserId(int userId) {
        return psyTestRecordMapper.selectByUserId(userId);
    }

    public int getHealthyCount(int userId) {
        return psyTestRecordMapper.getHealthyCount(userId);
    }

    public int getMildCount(int userId) {
        return psyTestRecordMapper.getMildCount(userId);
    }

    public int getModerateCount(int userId) {
        return psyTestRecordMapper.getModerateCount(userId);
    }

    public int getSevereCount(int userId) {
        return psyTestRecordMapper.getSevereCount(userId);
    }
}
